package itsm.isperp.workflow.listener;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.workflow.delegate.DelegateInfo;
import itsm.isperp.workflow.service.DelegateService;
import itsm.isperp.workflow.utils.BpmConst;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;

public class DelegateTaskListener extends BaseTaskListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public boolean execute(DelegateTask delegateTask,
			DelegateExecution delegateExecution, String defId, String nodeId) {
		DelegateService delegateService = ContextHolder
				.getSpringBean(DelegateService.class);

		String assignee = delegateTask.getAssignee();
		String processDefinitionId = delegateTask.getProcessDefinitionId();
		DelegateInfo delegateInfo = delegateService.getDelegateInfo(assignee,
				processDefinitionId);

		if (delegateInfo == null) {
			return true;
		}

		String attorney = delegateInfo.getAttorney();
		delegateTask.setAssignee(attorney);

		delegateService.saveRecord(assignee, attorney, delegateTask.getId());

		return true;
	}

	@Override
	protected String getScriptType() {
		return BpmConst.AssignScript;
	}
}
